package com.study.concurrent.cyclicbarrier;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class CycWorker implements Runnable {

	final int id;
	final CyclicBarrier barrier;
	
	public CycWorker(final int id, final CyclicBarrier barrier){
		this.id = id;
		this.barrier = barrier;
	}
	
	@Override
	public void run() {
		try {  
            System.out.println(this.id + "starts to run !");  
            Thread.sleep((long) (Math.random() * 10000));  
            System.out.println(this.id + "arrived !");  
            this.barrier.await();  
        } catch (InterruptedException e) {  
            e.printStackTrace();  
        } catch (BrokenBarrierException e) {  
            e.printStackTrace();  
        }  
	}
	
}
